<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<div class="easyui-layout" fit="true">
    <shiro:hasPermission name="permission:manage">
        <div data-options="region:'east',title:'权限',split:true,collapsible:true" style="width:500px;">
            <table id="menumanage_permission_treegrid" style="width:100%;"></table>
        </div>
    </shiro:hasPermission>
    <div data-options="region:'center',title:'菜单管理'">
        <table id="menumanage_datagrid"></table>
    </div>
</div>
<shiro:hasPermission name="menu:add">
    <div id="addmenu_btn">
        <a href="${pageContext.request.contextPath}/admin/menu/add.html" class="easyui-linkbutton" iconCls="icon-add">
            添加菜单</a>
    </div>
</shiro:hasPermission>
<shiro:hasPermission name="menu:grant">
    <div id="role_grant_btn">
        <a onclick="menumanage.grantAuthen()" href="javascript:void(0)" class="easyui-linkbutton"
           iconCls="icon-add">
            授权
        </a>
    </div>
</shiro:hasPermission>
<script type="text/javascript">
    top.setMenuActionOpen('系统设置', '菜单管理');
    var menumanage = {
        menuGridInit: function () {
            $('#menumanage_datagrid').treegrid({
                url: '${pageContext.request.contextPath}/admin/menu/getMenuManageInfoAjax.html',
                fitColumns: true,
                striped: true,
                idField: "id",
                rownumbers: true,
                singleSelect: true,
                treeField: 'name',
                striped: true,
                method: 'post',
                animate: true,
                lines: true,
                fit: true,
                toolbar: '#addmenu_btn',
                columns: [[
                    {field: 'name', title: '名字', width: '30%'},
                    {field: 'url', title: '链接', width: '40%'},
                    {field: 'order', title: '排序', width: '5%'},
                    {
                        field: 'action',
                        title: '操作',
                        hidden: true,
                        width: '15%',
                        formatter: function (value, row, index) {
                            var _actionbtn = "";
                            if (GlobalPermission.menu_add) {
                                _actionbtn += "&nbsp;<a href='${pageContext.request.contextPath}/admin/menu/add.html?parentId=" + row.id + "'  title='添加'>[添加]</a>&nbsp;";
                            }
                            if (GlobalPermission.menu_edit) {
                                _actionbtn += "&nbsp;<a href='${pageContext.request.contextPath}/admin/menu/edit/" + row.id + ".html'  title='编辑'>[编辑]</a>&nbsp;";
                            }
                            if (GlobalPermission.menu_del) {
                                _actionbtn += "&nbsp;<a href='javascript:void(0)' onclick='menumanage.deletemenu(" + row.id + ")' title='删除'>[删除]</a>&nbsp;";
                            }
                            return _actionbtn;
                        }
                    }
                ]],
                loadFilter: function (data) {
                    var error = data.error;
                    if (error) {
                        Common.error(error);
                        return;
                    }
                    return data.success;
                },
                onLoadError: function () {
                    LoadError();
                },
                onLoadSuccess: function (data) {
                    if (GlobalPermission.menu_add || GlobalPermission.menu_edit || GlobalPermission.menu_del) {
                        $('#menumanage_datagrid').treegrid('showColumn', 'action');
                    }

                },
                onClickRow: function (row) {

                },
                onClickCell:function(field,row){
                    if(field!='action'){
                        if (GlobalPermission.menu_permission) {
                            menumanage.queryAuthen(row.id);
                        }
                    }
                }

            });
        },
        permissionGridInit: function () {
            $('#menumanage_permission_treegrid').treegrid({
                url: '${pageContext.request.contextPath}/admin/permission/getManagePermissionsAjax.html',
                idField: 'id',
                treeField: 'name',
                fitColumns: true,
                striped: true,
                method: 'post',
                rownumbers: true,
                singleSelect: false,
                animate: true,
                lines: true,
                toolbar: '#role_grant_btn',
                columns: [[
                    {field: 'id', checkbox: true},
                    {title: '权限名字', field: 'name', width: 100},
                    {field: 'description', title: '权限描述', width: 100}
                ]],
                loadFilter: function (data) {
                    var error = data.error;
                    if (error) {
                        Common.error(error);
                        return;
                    }
                    return data.success;
                },
                onLoadError: function () {
                    LoadError();
                },
                onCheck: function (node) {
                    var parentPermissions = $('#menumanage_permission_treegrid').treegrid("getParent", node.id);
                    $(parentPermissions).each(function (i, v) {
                        $('#menumanage_permission_treegrid').treegrid("select", v.id);
                    });
                },
                onUncheck: function (node) {
                    var childrenPermissions = $('#menumanage_permission_treegrid').treegrid("getChildren", node.id);
                    $(childrenPermissions).each(function (i, v) {
                        $('#menumanage_permission_treegrid').treegrid("unselect", v.id);
                    });
                },
                onLoadSuccess: function (data) {
                }
            });
        },
        init: function () {
            menumanage.menuGridInit();
            menumanage.permissionGridInit();
        },
        deletemenu: function (id) {
            var menuIdArray = new Array();
            var treegrid_children = $('#menumanage_datagrid').treegrid("getChildren", id);
            $(treegrid_children).each(function (i, v) {
                menuIdArray.push(v.id);
            });
            menuIdArray.push(id);

            Common.confirm("你确定要删除所选菜单吗？", function (r) {
                if (r) {
                    $.ajax({
                        url: '${pageContext.request.contextPath}/admin/menu/deleteAjax.html',
                        data: {"id": menuIdArray},
                        success: function (data) {
                            var error = data.error;
                            if (error) {
                                Common.error(error);
                                return;
                            }
                            Common.info(data.success);
                            $('#menumanage_datagrid').treegrid('load');
                        }
                    });
                }
            })
        },
        queryAuthen: function (id) {
            $.ajax({
                url: '${pageContext.request.contextPath}/admin/menupermission/getPermissionIdsByMenuIdAjax.html',
                data: {"id": id},
                success: function (data) {
                    var error = data.error;
                    if (error) {
                        Common.error(error);
                        return;
                    }
                    $('#menumanage_permission_treegrid').treegrid("unselectAll");
                    $(data.success).each(function (i, v) {
                        $('#menumanage_permission_treegrid').treegrid("select", v);
                    });
                }
            });
        },
        grantAuthen: function () {
            var menuSelected = $('#menumanage_datagrid').treegrid('getSelected');
            if (!menuSelected) {
                Common.notice("请选择菜单分配权限")
                return;
            }
            var menuId = menuSelected.id;
            var perIds = new Array();
            var perSelected = $('#menumanage_permission_treegrid').treegrid("getSelections");
            $(perSelected).each(function (i, v) {
                perIds.push(v.id);
            });
            if (perIds.length <= 0) {
                perIds.push(0);
            }
            Common.confirm("你确定要授权吗？", function (r) {
                if (r) {
                    $.ajax({
                        url: '${pageContext.request.contextPath}/admin/menupermission/grantmenuPermissionsAjax.html',
                        data: {"menuId": menuId, perIds: perIds},
                        success: function (data) {
                            var error = data.error;
                            if (error) {
                                Common.error(error);
                                return;
                            }
                            Common.info(data.success);
                        }
                    });
                }
            });
        }

    };
    menumanage.init();
</script>
</body>
</html>
